package com.kma.summer2012.facade;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.Properties;

public class PasswordRecoveryFacadeImpl implements PasswordRecoveryFacade {
    private Session m_Session;
    private Message m_simpleMessage;
    private InternetAddress m_fromAddress;
    private InternetAddress m_toAddress;
    private Properties m_properties;

    @Override
    public void sendMail(String mail, String path) throws Exception {
        try {

            m_properties = new Properties();
            //m_properties.put("mail.smtp.host", "smtp.gmail.com");
            // m_properties.put("mail.smtp.socketFactory.port", "465");
            // m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            m_properties.put("mail.smtps.auth", "true");
            //  m_properties.put("mail.smtp.port", "465");
            m_properties.put("mail.debug", "true");
            // m_properties.put("mail.smtp.ssl.enable", "true");


            m_Session = Session.getInstance(m_properties, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("crumbs.anastazi", "crumbs12345"); // username and the password
                }

            });

            m_simpleMessage = new MimeMessage(m_Session);

            m_fromAddress = new InternetAddress("crumbs.anastazi@gmail.com");
            m_toAddress = new InternetAddress(mail);


            m_simpleMessage.setFrom(m_fromAddress);
            m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
            m_simpleMessage.setSubject("Change Password");
            m_simpleMessage.setContent("To change your password, please follow the link: " + path, "text/plain");

            Transport transport = m_Session.getTransport("smtps");
            try {
                transport.connect("smtp.gmail.com", 465, "crumbs.anastazi", "crumbs12345");
                transport.sendMessage(m_simpleMessage, m_simpleMessage.getAllRecipients());
            } finally {
                transport.close();
            }
            //Transport.send(m_simpleMessage);

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }




     /*   InitialContext ctx = new InitialContext();
        Session session =
                (Session) ctx.lookup("mail/kma_project");

        // Create email and headers.
        Message msg = new MimeMessage(session);
        msg.setSubject("My Subject");
        msg.setRecipient(RecipientType.TO,
                new InternetAddress(
                        mail));
       /* msg.setRecipient(RecipientType.CC,
                new InternetAddress(
                        "michelle@email.com",
                        "Michelle"));
        msg.setFrom(new InternetAddress(
                "crumbs.anastazi@gmail.com",
                "Anastazi"));
        /*
        // Body text.
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("Here are the files.");

        // Multipart message.
       // Multipart multipart = new MimeMultipart();
       // multipart.addBodyPart(messageBodyPart);

        // Attachment file from string.
        messageBodyPart = new MimeBodyPart();
        messageBodyPart.setFileName("README1.txt");
        messageBodyPart.setContent(new String(
                "file 1 content"),
                "text/plain");
        multipart.addBodyPart(messageBodyPart);

        // Attachment file from file.
        messageBodyPart = new MimeBodyPart();
        messageBodyPart.setFileName("README2.txt");
        DataSource src = new FileDataSource("file.txt");
        messageBodyPart.setDataHandler(new DataHandler(src));
        multipart.addBodyPart(messageBodyPart);

        // Attachment file from byte array.
        messageBodyPart = new MimeBodyPart();
        messageBodyPart.setFileName("README3.txt");
        src = new ByteArrayDataSource(
                "file 3 content".getBytes(),
                "text/plain");
        messageBodyPart.setDataHandler(new DataHandler(src));
        multipart.addBodyPart(messageBodyPart);

        // Add multipart message to email.
        msg.setContent(multipart);
         */
    // Send email.
        /*msg.setText("Hi, this is your message!");
        Transport.send(msg);
    }
                          */


}
